package test_achieve;
import cucumber.api.PendingException;
import cucumber.api.java.en.Given;
import util.ConfigSql;
import util.JdbcUtil;
import util.tools.BusinessTools;

/**
 * Created by chenkuo on 2018/4/12.
 */
public class DatabaseRead {
    @Given("^用户修改等级：(.+)：(.+)$")
    public void 用户修改等级(String user,String grade) throws Throwable {
        String userId =获取用户userId(user);
        JdbcUtil.sqlExecute(ConfigSql.get("upatateUserLevel",grade,userId));
    }

    @Given("^数据库增加用户余额：(.+)：(.+)$")
    public void 数据库增加用户余额(String user, String addBalance) throws Throwable {
        String userId =获取用户userId(user);
            JdbcUtil.sqlExecute(ConfigSql.get("upatateUserBalancel",addBalance,userId));
            JdbcUtil.sqlExecute(ConfigSql.get("upatateUserBalance2",addBalance,userId));
    }

    @Given("^获取用户userId：(.+)$")
    public String 获取用户userId(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String userId = JdbcUtil.sqlString(ConfigSql.get("queryUer1UserId",userSecret));
        if (userId == null){
            userId = JdbcUtil.sqlString(ConfigSql.get("queryUer2UserId",userSecret));
        }
        System.out.println(userId);
        return userId;
    }

    @Given("^修改闪送员站点：(.*)：(.*)$")
    public void 修改闪送员站点(String phone,String name) throws Throwable {
        String phoneSecret = BusinessTools.getMobileSecret(phone);
        String sql = "update courier1.couriers set train_addr_id = (SELECT id from courier1.train_addresses where city_id = 1101 and name = '"+name+"')where user_id = (SELECT id from user2.users where mobile = '"+phoneSecret+"' and type = 2 limit 1)";
    JdbcUtil.sqlExecute(sql);
    }

    @Given("^获取闪送员courierId：(.+)$")
    public String 获取闪送员courierId(String user) throws Throwable {
        String userSecret = BusinessTools.getMobileSecret(user);
        String userId = JdbcUtil.sqlString(ConfigSql.get("queryUser1CourierId",userSecret));
        if (userId == null){
            userId = JdbcUtil.sqlString(ConfigSql.get("queryUser2CourierId",userSecret));
        }
        System.out.println("courierId:"+userId);
        return userId;
    }

    @Given("^增大闪送员的重要通知阈值：(.*)：(.*)$")
    public void 增大闪送员的重要通知阈值(String user,String number) throws Throwable {
        String courierId = 获取闪送员courierId(user);
        String sql = "UPDATE strategy1.courier_read_messages set major_notice_index = "+number+" WHERE courier_id = "+courierId+"";
        JdbcUtil.sqlExecute(sql);
    }
}
